//
//  UIButtonImageWithLabel.m
//  Molian
//
//  Created by 小蚕科技 on 15/6/9.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "UIButtonImageWithLabel.h"

@implementation UIButtonImageWithLabel

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void) setImage:(UIImage *)image withTitle:(NSString *)title forState:(UIControlState)stateType {
    
    //UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
    CGSize titleSize = [title sizeWithFont:[UIFont fontWithName:FontName size:12]];
    [self.imageView setContentMode:UIViewContentModeCenter];
    [self setImageEdgeInsets:UIEdgeInsetsMake(-8.0,
                                              0.0,
                                              0.0,
                                              -titleSize.width)];
    [self setImage:image forState:stateType];
    
    [self.titleLabel setContentMode:UIViewContentModeCenter];
    [self.titleLabel setBackgroundColor:[UIColor clearColor]];
    [self.titleLabel setFont:[UIFont fontWithName:FontName size:12]];
    [self.titleLabel setTextColor:[UIColor whiteColor]];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(image.size.height+12,
                                              -image.size.width,
                                              0.0,
                                              0.0)];
    [self setTitle:title forState:stateType];
    
}


//备注：如果不需要上下显示，只需要横向排列的时候，就不需要设置左右偏移量了，代码如下
//
//- (void) setImage:(UIImage *)image withTitle:(NSString *)title forState:(UIControlState)stateType {
//    
//    //UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
//    
//    
//    
//    CGSize titleSize = [title sizeWithFont:HELVETICANEUEMEDIUM_FONT(12.0f)];
//    
//    [self.imageView setContentMode:UIViewContentModeCenter];
//    
//    [self setImageEdgeInsets:UIEdgeInsetsMake(-8.0,
//                                              
//                                              0.0,
//                                              
//                                              0.0,
//                                              
//                                              0.0)];
//    
//    [self setImage:image forState:stateType];
//    
//    
//    
//    [self.titleLabel setContentMode:UIViewContentModeCenter];
//    
//    [self.titleLabel setBackgroundColor:[UIColor clearColor]];
//    
//    [self.titleLabel setFont:HELVETICANEUEMEDIUM_FONT(12.0f)];
//    
//    [self.titleLabel setTextColor:COLOR_ffffff];
//    
//    [self setTitleEdgeInsets:UIEdgeInsetsMake(30.0,
//                                              
//                                              0.0, 
//                                              
//                                              0.0,
//                                              
//                                              0.0)];
//    
//    [self setTitle:title forState:stateType];
//    
//}
@end
